//
// Created by zhengjuping on 24-7-10.
//

#ifndef ENV_II_BMP280_H
#define ENV_II_BMP280_H

enum {
    BMP280_REGISTER_DIG_T1       = 0x88,
    BMP280_REGISTER_DIG_T2       = 0x8A,
    BMP280_REGISTER_DIG_T3       = 0x8C,
    BMP280_REGISTER_DIG_P1       = 0x8E,
    BMP280_REGISTER_DIG_P2       = 0x90,
    BMP280_REGISTER_DIG_P3       = 0x92,
    BMP280_REGISTER_DIG_P4       = 0x94,
    BMP280_REGISTER_DIG_P5       = 0x96,
    BMP280_REGISTER_DIG_P6       = 0x98,
    BMP280_REGISTER_DIG_P7       = 0x9A,
    BMP280_REGISTER_DIG_P8       = 0x9C,
    BMP280_REGISTER_DIG_P9       = 0x9E,
    BMP280_REGISTER_CHIPID       = 0xD0,
    BMP280_REGISTER_VERSION      = 0xD1,
    BMP280_REGISTER_SOFTRESET    = 0xE0,
    BMP280_REGISTER_CAL26        = 0xE1, /**< R calibration = 0xE1-0xF0 */
    BMP280_REGISTER_STATUS       = 0xF3,
    BMP280_REGISTER_CONTROL      = 0xF4,
    BMP280_REGISTER_CONFIG       = 0xF5,
    BMP280_REGISTER_PRESSUREDATA = 0xF7,
    BMP280_REGISTER_TEMPDATA     = 0xFA,
};

typedef struct {
    uint16_t dig_T1; /**< dig_T1 cal register. */
    int16_t dig_T2;  /**<  dig_T2 cal register. */
    int16_t dig_T3;  /**< dig_T3 cal register. */

    uint16_t dig_P1; /**< dig_P1 cal register. */
    int16_t dig_P2;  /**< dig_P2 cal register. */
    int16_t dig_P3;  /**< dig_P3 cal register. */
    int16_t dig_P4;  /**< dig_P4 cal register. */
    int16_t dig_P5;  /**< dig_P5 cal register. */
    int16_t dig_P6;  /**< dig_P6 cal register. */
    int16_t dig_P7;  /**< dig_P7 cal register. */
    int16_t dig_P8;  /**< dig_P8 cal register. */
    int16_t dig_P9;  /**< dig_P9 cal register. */
} bmp280_calib_data;


class BMP280{

public:
    explicit BMP280();
    ~BMP280();
    int32_t getValue( float &pressure, float &temperature);

private:
    int32_t read16(uint8_t addr, uint8_t reg, uint16_t &data);
    int32_t read16_LE(uint8_t addr, uint8_t reg, uint16_t &data);
    int32_t readS16(uint8_t addr, uint8_t reg, int16_t &data);
    int32_t readS16_LE(uint8_t addr, uint8_t reg, int16_t &data);
    int32_t read24(uint8_t addr, uint8_t reg, uint32_t &data);
    int32_t write8(uint8_t addr, uint8_t reg, uint8_t data);

    void setSampling(uint8_t addr);
    void readCoefficients(uint8_t addr);
    int32_t readTemperatureData(uint8_t addr, float &Temperature);
    int32_t readPressureData(uint8_t addr, float &Pressure);
    int32_t mTfine;
    bmp280_calib_data mBmp280Calib;
};



#endif //ENV_II_BMP280_H
